<template>
  <div>
    <van-search
      @click="$router.push('/search')"
      v-model="value"
      disabled
      placeholder="请输入搜索关键词"
    />
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <template v-for="item in bookList">
        <book-card
          @onClick="onDetailClick(item)"
          :key="item.id"
          :item="item"
        ></book-card>
      </template>
    </van-list>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex'
import BookCard from '@/components/book-card.vue'
export default {
  name: 'home',
  components: { BookCard },
  data() {
    return {
      opt: {
        page: 0,
        size: 10,
      },
      finished: false,
      loading: false,
      value: '',
    }
  },
  activated() {},

  computed: {
    ...mapState(['bookList']),
  },
  methods: {
    ...mapActions(['fetchBookList']),
    async onLoad() {
      this.opt.page++
      const data = await this.fetchBookList(this.opt)
      // 关闭loading
      this.loading = false
      if (this.bookList.length >= 50) {
        this.finished = true
      }
      console.log('bookList:', data)
    },
    onDetailClick(item) {
      this.$router.push(`/detail/${item.id}`)
    },
  },
}
</script>

<style></style>
